/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-03-27     Administrator       the first version
 */
#ifndef _SYS_H
#define _SYS_H
#include "stm32f4xx.h"
#include <rtthread.h>

typedef int32_t  s32;
typedef int16_t s16;
typedef int8_t  s8;

typedef const int32_t sc32;
typedef const int16_t sc16;
typedef const int8_t sc8;

typedef __IO int32_t  vs32;
typedef __IO int16_t  vs16;
typedef __IO int8_t   vs8;

typedef __I int32_t vsc32;
typedef __I int16_t vsc16;
typedef __I int8_t vsc8;

typedef uint32_t  u32;
typedef uint16_t u16;
typedef uint8_t  u8;

typedef const uint32_t uc32;
typedef const uint16_t uc16;
typedef const uint8_t uc8;

typedef __IO uint32_t  vu32;
typedef __IO uint16_t vu16;
typedef __IO uint8_t  vu8;

typedef __I uint32_t vuc32;
typedef __I uint16_t vuc16;
typedef __I uint8_t vuc8;

typedef struct
{
    float x;
    float y;
    float z;
}F_XYZ;
typedef struct
{
    uint16_t x;
    uint16_t y;
    uint16_t z;
}U16_XYZ;
//位带操作,实现51类似的GPIO控制功能
//具体实现思想,参考<<CM3权威指南>>第五章(87页~92页).M4同M3类似,只是寄存器地址变了.
//IO口操作宏定义
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr)  *((volatile unsigned long  *)(addr))
#define BIT_ADDR(addr, bitnum)   MEM_ADDR(BITBAND(addr, bitnum))
//IO口地址映射
#define GPIOA_ODR_Addr    (GPIOA_BASE+20) //0x40020014
#define GPIOB_ODR_Addr    (GPIOB_BASE+20) //0x40020414
#define GPIOC_ODR_Addr    (GPIOC_BASE+20) //0x40020814
#define GPIOD_ODR_Addr    (GPIOD_BASE+20) //0x40020C14
#define GPIOE_ODR_Addr    (GPIOE_BASE+20) //0x40021014
#define GPIOF_ODR_Addr    (GPIOF_BASE+20) //0x40021414
#define GPIOG_ODR_Addr    (GPIOG_BASE+20) //0x40021814
#define GPIOH_ODR_Addr    (GPIOH_BASE+20) //0x40021C14
#define GPIOI_ODR_Addr    (GPIOI_BASE+20) //0x40022014
#define GPIOJ_ODR_ADDr    (GPIOJ_BASE+20) //0x40022414
#define GPIOK_ODR_ADDr    (GPIOK_BASE+20) //0x40022814

#define GPIOA_IDR_Addr    (GPIOA_BASE+16) //0x40020010
#define GPIOB_IDR_Addr    (GPIOB_BASE+16) //0x40020410
#define GPIOC_IDR_Addr    (GPIOC_BASE+16) //0x40020810
#define GPIOD_IDR_Addr    (GPIOD_BASE+16) //0x40020C10
#define GPIOE_IDR_Addr    (GPIOE_BASE+16) //0x40021010
#define GPIOF_IDR_Addr    (GPIOF_BASE+16) //0x40021410
#define GPIOG_IDR_Addr    (GPIOG_BASE+16) //0x40021810
#define GPIOH_IDR_Addr    (GPIOH_BASE+16) //0x40021C10
#define GPIOI_IDR_Addr    (GPIOI_BASE+16) //0x40022010
#define GPIOJ_IDR_Addr    (GPIOJ_BASE+16) //0x40022410
#define GPIOK_IDR_Addr    (GPIOK_BASE+16) //0x40022810

//IO口操作,只对单一的IO口!
//确保n的值小于16!
#define PAout(n)   BIT_ADDR(GPIOA_ODR_Addr,n)  //输出
#define PAin(n)    BIT_ADDR(GPIOA_IDR_Addr,n)  //输入

#define PBout(n)   BIT_ADDR(GPIOB_ODR_Addr,n)  //输出
#define PBin(n)    BIT_ADDR(GPIOB_IDR_Addr,n)  //输入

#define PCout(n)   BIT_ADDR(GPIOC_ODR_Addr,n)  //输出
#define PCin(n)    BIT_ADDR(GPIOC_IDR_Addr,n)  //输入

#define PDout(n)   BIT_ADDR(GPIOD_ODR_Addr,n)  //输出
#define PDin(n)    BIT_ADDR(GPIOD_IDR_Addr,n)  //输入

#define PEout(n)   BIT_ADDR(GPIOE_ODR_Addr,n)  //输出
#define PEin(n)    BIT_ADDR(GPIOE_IDR_Addr,n)  //输入

#define PFout(n)   BIT_ADDR(GPIOF_ODR_Addr,n)  //输出
#define PFin(n)    BIT_ADDR(GPIOF_IDR_Addr,n)  //输入

#define PGout(n)   BIT_ADDR(GPIOG_ODR_Addr,n)  //输出
#define PGin(n)    BIT_ADDR(GPIOG_IDR_Addr,n)  //输入

#define PHout(n)   BIT_ADDR(GPIOH_ODR_Addr,n)  //输出
#define PHin(n)    BIT_ADDR(GPIOH_IDR_Addr,n)  //输入

#define PIout(n)   BIT_ADDR(GPIOI_ODR_Addr,n)  //输出
#define PIin(n)    BIT_ADDR(GPIOI_IDR_Addr,n)  //输入

#define PJout(n)   BIT_ADDR(GPIOJ_ODR_Addr,n)  //输出
#define PJin(n)    BIT_ADDR(GPIOJ_IDR_Addr,n)  //输入

#define PKout(n)   BIT_ADDR(GPIOK_ODR_Addr,n)  //输出
#define PKin(n)    BIT_ADDR(GPIOK_IDR_Addr,n)  //输入
//////////////////////////////////////////////////////////////////////////////////
#define GPIOA_CLOCK_INIT   (RCC->AHB1ENR|=1<<0)
#define GPIOB_CLOCK_INIT   (RCC->AHB1ENR|=1<<1)
#define GPIOC_CLOCK_INIT   (RCC->AHB1ENR|=1<<2)
#define GPIOD_CLOCK_INIT   (RCC->AHB1ENR|=1<<3)
#define GPIOE_CLOCK_INIT   (RCC->AHB1ENR|=1<<4)
#define GPIOF_CLOCK_INIT   (RCC->AHB1ENR|=1<<5)
#define GPIOG_CLOCK_INIT   (RCC->AHB1ENR|=1<<6)
#define DMA1_CLOCK_INIT    (RCC->AHB1ENR|=1<<21)
#define DMA2_CLOCK_INIT    (RCC->AHB1ENR|=1<<22)
#define DCMI_CLOCK_INIT    (RCC->AHB2ENR|=1<<0)
#define FSMC_CLOCK_INIT    (RCC->AHB3ENR|=1<<0)

#define TIM2_CLOCK_INIT    (RCC->APB1ENR|=1<<0)
#define TIM3_CLOCK_INIT    (RCC->APB1ENR|=1<<1)
#define TIM4_CLOCK_INIT    (RCC->APB1ENR|=1<<2)
#define TIM5_CLOCK_INIT    (RCC->APB1ENR|=1<<3)
#define TIM6_CLOCK_INIT    (RCC->APB1ENR|=1<<4)
#define TIM7_CLOCK_INIT    (RCC->APB1ENR|=1<<5)
#define TIM12_CLOCK_INIT   (RCC->APB1ENR|=1<<6)
#define TIM13_CLOCK_INIT   (RCC->APB1ENR|=1<<7)
#define TIM14_CLOCK_INIT   (RCC->APB1ENR|=1<<8)
#define WWDG_CLOCK_INIT    (RCC->APB1ENR|=1<<11)
#define SPI2_CLOCK_INIT    (RCC->APB1ENR|=1<<14)
#define SPI3_CLOCK_INIT    (RCC->APB1ENR|=1<<15)
#define USART2_CLOCK_INIT  (RCC->APB1ENR|=1<<17)
#define USART3_CLOCK_INIT  (RCC->APB1ENR|=1<<18)
#define UART4_CLOCK_INIT   (RCC->APB1ENR|=1<<19)
#define UART5_CLOCK_INIT   (RCC->APB1ENR|=1<<20)
#define I2C1_CLOCK_INIT    (RCC->APB1ENR|=1<<21)
#define I2C2_CLOCK_INIT    (RCC->APB1ENR|=1<<22)
#define I2C3_CLOCK_INIT    (RCC->APB1ENR|=1<<23)
#define CAN1_CLOCK_INIT    (RCC->APB1ENR|=1<<25)
#define CAN2_CLOCK_INIT    (RCC->APB1ENR|=1<<26)
#define PWR_CLOCK_INIT     (RCC->APB1ENR|=1<<28)
#define DAC_CLOCK_INIT     (RCC->APB1ENR|=1<<29)
#define TIM1_CLOCK_INIT    (RCC->APB2ENR|=1<<0)
#define TIM8_CLOCK_INIT    (RCC->APB2ENR|=1<<1)
#define USART1_CLOCK_INIT  (RCC->APB2ENR|=1<<4)
#define USART6_CLOCK_INIT  (RCC->APB2ENR|=1<<5)
#define ADC1_CLOCK_INIT    (RCC->APB2ENR|=1<<8)
#define ADC2_CLOCK_INIT    (RCC->APB2ENR|=1<<9)
#define ADC3_CLOCK_INIT    (RCC->APB2ENR|=1<<10)
#define SDIO_CLOCK_INIT    (RCC->APB2ENR|=1<<11)
#define SPI1_CLOCK_INIT    (RCC->APB2ENR|=1<<12)
#define SYSCFG_CLOCK_INIT  (RCC->APB2ENR|=1<<14)
#define TIM9_CLOCK_INIT    (RCC->APB2ENR|=1<<16)
#define TIM10_CLOCK_INIT   (RCC->APB2ENR|=1<<17)
#define TIM11_CLOCK_INIT   (RCC->APB2ENR|=1<<18)
//////////////////////////////////////////////////////////////////////////////////
//AF0:MCO/SWD/SWCLK/RTC   AF1:TIM1/TIM2;            AF2:TIM3~5;               AF3:TIM8~11
//AF4:I2C1~I2C3;          AF5:SPI1/SPI2;            AF6:SPI3;                 AF7:USART1~3;
//AF8:USART4~6;           AF9;CAN1/CAN2/TIM12~14    AF10:USB_OTG/USB_HS       AF11:ETH
//AF12:FSMC/SDIO/OTG/HS   AF13:DCIM                 AF14:                     AF15:EVENTOUT
#define RTC_AF 0
#define SWD_AF 0
#define SWCLK_AF 0
#define MCO_AF 0
#define TIM1_AF 1
#define TIM2_AF 1
#define TIM3_AF 2
#define TIM4_AF 2
#define TIM5_AF 2
#define TIM8_AF 3
#define TIM9_AF 3
#define TIM10_AF 3
#define TIM11_AF 3
#define I2C1_AF 4
#define I2C2_AF 4
#define I2C3_AF 4
#define SPI1_AF 5
#define SPI2_AF 5
#define SPI3_AF 6
#define USART1_AF 7
#define USART2_AF 7
#define USART3_AF 7
#define UART4_AF 8
#define UART5_AF 8
#define UART6_AF 8
#define CAN1_AF 9
#define CAN2_AF 9
#define TIM12_AF 9
#define TIM13_AF 9
#define TIM14_AF 9
#define USBOTG_AF 10
#define USBHS_AF 10
#define ETH_AF 11
#define FSMC_AF 12
#define SDIO_AF 12
#define OTG_AF 12
#define HS_AF 12
#define DCMI_AF 13
#define EVENTOUT_AF 15
//Ex_NVIC_Config专用定义
#define GPIO_A              0
#define GPIO_B              1
#define GPIO_C              2
#define GPIO_D              3
#define GPIO_E              4
#define GPIO_F              5
#define GPIO_G              6
#define GPIO_H              7
#define GPIO_I              8

#define FTIR                1       //下降沿触发
#define RTIR                2       //上升沿触发

//GPIO设置专用宏定义
#define GPIO_MODE_IN        0       //普通输入模式
#define GPIO_MODE_OUT       1       //普通输出模式
#define GPIO_MODE_AF        2       //AF功能模式
#define GPIO_MODE_AIN       3       //模拟输入模式

#define GPIO_SPEED_2M       0       //GPIO速度2Mhz
#define GPIO_SPEED_25M      1       //GPIO速度25Mhz
#define GPIO_SPEED_50M      2       //GPIO速度50Mhz
#define GPIO_SPEED_100M     3       //GPIO速度100Mhz

#define GPIO_PUPD_NONE      0       //不带上下拉
#define GPIO_PUPD_PU        1       //上拉
#define GPIO_PUPD_PD        2       //下拉
#define GPIO_PUPD_RES       3       //保留

#define GPIO_OTYPE_PP       0       //推挽输出
#define GPIO_OTYPE_OD       1       //开漏输出

//GPIO引脚编号定义
#define PIN0                1<<0
#define PIN1                1<<1
#define PIN2                1<<2
#define PIN3                1<<3
#define PIN4                1<<4
#define PIN5                1<<5
#define PIN6                1<<6
#define PIN7                1<<7
#define PIN8                1<<8
#define PIN9                1<<9
#define PIN10               1<<10
#define PIN11               1<<11
#define PIN12               1<<12
#define PIN13               1<<13
#define PIN14               1<<14
#define PIN15               1<<15


void MY_NVIC_SetVectorTable(u32 NVIC_VectTab, u32 Offset);  //设置偏移地址
void MY_NVIC_PriorityGroupConfig(u8 NVIC_Group);            //设置NVIC分组
void MY_NVIC_Init(u8 NVIC_PreemptionPriority,u8 NVIC_SubPriority,u8 NVIC_Channel,u8 NVIC_Group);//设置中断
void Ex_NVIC_Config(u8 GPIOx,u8 BITx,u8 TRIM);              //外部中断配置函数(只对GPIOA~I)
void GPIO_AF_Set(GPIO_TypeDef* GPIOx,u8 BITx,u8 AFx);       //GPIO复用功能设置
void GPIO_Set(GPIO_TypeDef* GPIOx,u32 BITx,u32 MODE,u32 OTYPE,u32 OSPEED,u32 PUPD);//GPIO设置函数
#endif


